package org.huangrui.spark.java.core.rdd.operate.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

/**
 * @Author hr
 * @Create 2024-10-17 17:44
 */
public class Spark11_Operate_Transform_KV_wordCount {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);
        // TODO groupByKey方法作用是将KV类型的数据直接按照K对V进行分组
        //     (a, [1,3])
        //     (b, [2,4])
        jsc.parallelizePairs(
                Arrays.asList(
                        new Tuple2<>("a", 1),
                        new Tuple2<>("b", 2),
                        new Tuple2<>("a", 3),
                        new Tuple2<>("b", 4)
                )
        ).groupByKey().mapValues(v1 -> {
            Integer sum = 0;
            for (Integer num : v1) {
                sum += num;
            }
            return sum;
        }).collect().forEach(System.out::println);

        jsc.close();
    }
}
